import shortid from 'shortid'
import clipboard from 'clipboard';

export function getChinaWeek(index) {
  var weeks = new Array(
    '星期日',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六'
  )
  return weeks[index]
}

export function matchNum(num) {
  let reg = /^(\-|\+)?\d+\.?\d{0,10}$/
  if (!reg.test(num)) {
    return false
  } else {
    return true
  }
}

export function getRandomNum(max, min) {
  return Math.floor(Math.random() * (max - min) + min)
}

/**
 * Generate shortId
 */
export const generateId = prefix => {
  const id = shortid.generate()
  return prefix ? `${prefix}_${id}` : id
}

export function getUrlEncryptParams(url) {
  let params = getUrlParams(url)
  let paramArr = []
  Object.keys(params).forEach(key => {
    if (key) {
      paramArr.push({
        key: key,
        value: params[key]
      })
    }
  })
  return paramArr
}

export function camelize(styleName) {
  var parts = styleName.split('-'),
    len = parts.length
  if (len == 1) {
    return parts[0]
  }
  //下面就要处理-moz--box-shadow 这样的第一个字符是-的
  var camelized =
    styleName.charAt(0) == '-' ?
      parts[0].charAt(0).toUpperCase() + parts[0].substring(1) :
      parts[0]
  //循环处理 ---注意第一个已经处理过了 所以i从1开始
  for (var i = 1; i < len; i++) {
    cameplize += parts[i].charAt(0).toUpperCase() + parts[i].substring(1)
  }
  return camelized
}

export function getUUID() {
  return randomNums() + randomNums() + randomNums() + randomNums()
}

function randomNums() {
  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
}

export function getRandomInRange(min, max) {
  // 处理输入顺序错误（如min > max）
  [min, max] = [Math.min(min, max), Math.max(min, max)];
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

export function replaceBr(str) {
  if (str) {
    return str.replace(/\n/g, '<br/>')
  }
  return str;
}

export function copy(key, that, msg) {
  var clipboardObj = new clipboard(key)
  if (!msg) {
    msg = '复制成功'
  }
  that.$nextTick(() => {
    clipboardObj.on('success', (e) => {
      that.$modal.msgSuccess(msg)
      // 释放内存
      clipboardObj.destroy()
    })
    clipboardObj.on('error', (e) => {
      that.$modal.msgError('该浏览器不支持自动复制')
      // 释放内存
      clipboardObj.destroy()
    })
  })
}
